package com.coder520.mamabike.utils;

import com.coder520.mamabike.common.Parameter;
import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created by ASUS on 2017/8/16.
 */
public class QiNiuUpLoadUtil {

    @Autowired
    private static Parameter parameter;

    public static String uploadImg(MultipartFile file) throws IOException {

        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(configuration);

        Auth auth = Auth.create(parameter.getAccessKey(),parameter.getSecretKey());

        String upToken = auth.uploadToken(parameter.getPreff());

        Response response = uploadManager.put(file.getBytes(),null,upToken);

        DefaultPutRet defaultPutRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return defaultPutRet.key;
    }
}
